/**
 * 补零
 *
 * @export
 * @param {*} num
 * @return {*} 
 */
export function toZero(num) {
  return Number(num).toString().padStart(2,'0');
}
/**
 * 打开邮箱
 *
 * @export
 * @param {*} email
 */
export function openEmail(email) {
  window.open(`mailto:${email}`);
}
/**
 * 获取（当前）时间
 *
 * @export
 * @param {*} [time=new Date()]
 * @return {*} 
 */
export function newTime(time = new Date()) {
  let date = new Date(time);
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  let hour = date.getHours();
  let minute = date.getMinutes();
  let second = date.getSeconds();
  return {
    year,
    month,
    day,
    hour,
    minute,
    second,
    fullTime: `${year}-${toZero(month)}-${toZero(day)} ${toZero(hour)}:${toZero(minute)}:${toZero(second)}`
  };
}


/**
 * 深拷贝
 *
 * @export
 * @param {*} obj
 * @param {*} [cache=new WeakMap()]
 * @return {*} 
 */
export function deepClone(obj,cache = new WeakMap()) {
  if(obj === null) return null;
  if (obj instanceof Date) return new Date(obj);
  if (obj instanceof RegExp) return new RegExp(obj);
  if (obj instanceof Function) return new Function(obj);
  if (obj instanceof Number) return new Number(obj);
  if (obj instanceof String) return new String(obj);
  if (obj instanceof Boolean) return new Boolean(obj);
  if (obj instanceof Array) {
    let cloneArr = [];
    for (let i = 0; i < obj.length; i++) {
      if (cache.has(obj[i])) {
        cloneArr.push(cache.get(obj[i]));
      } else {
        cloneArr.push(deepClone(obj[i], cache));
      }
    }
    return cloneArr;
  }
  if (typeof obj !== "object") return obj;
  let cloneObj = {};
  for (let key in obj) {
    if (cache.has(obj[key])) {
      cloneObj[key] = cache.get(obj[key]);
    } else {
      cloneObj[key] = deepClone(obj[key], cache);
    }
  }
  return cloneObj;
}
/**
 * 分页器
 *
 * @export
 * @param {*} total
 * @param {*} pageSize
 * @param {*} currentPage
 * @return {*} 
 */
export function pagination(total, pageSize, currentPage) {
  let totalPage = Math.ceil(total / pageSize);
  let startPage = currentPage - 2;
  let endPage = currentPage + 2;
  if (startPage < 1) {
    startPage = 1;
  }
  if (endPage > totalPage) {
    endPage = totalPage;
  }
  let pageArr = [];
  for (let i = startPage || 1; i <= endPage; i++) {
    const item = {
      page: i,
      active: i === currentPage
    };
    pageArr.push(item);
  }
  return {
    list: pageArr,
    totalPage,
    startPage,
    endPage,
    currentPage,
    total
  };
}

// 列表数据分割
export function splitList(list, pageSize, currentPage) {
  let start = (currentPage - 1) * pageSize;
  let end = currentPage * pageSize;
  return list.slice(start, end);
}

